var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
var startX = 0, endX = 0, prevScrollLeft = 0, prevId = 0, prevIndex = 0;
var px = 0, py = 0, px2 = 0, py2 = 0, windowWidth = 0;
Page({
  data: {
    hasData: false,
    middle_height: "100vh",
    delivery_date: util.dateObjectformat(new Date(), 'yyyy-MM-dd'),
    delivery_date_text: "今天运力",
    delivery_data: [],
    detailShow: '',
    dd_style: "",
    path_data: [],
    alarm_type_text: '全部报警',
    tab: 6
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onShow: function (e) {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 10),
            warehouseId: res.data.value
          });
          that.loadData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--配送监控" });

    if (app.globalData.userType == 'schedule') {
      that.setData({
        is_schedule: true
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - (that.data.is_schedule ? 100 : 60),
          detail_height: res.windowHeight - 40
        });
        windowWidth = res.windowWidth;
      }
    });
    if (e.warehouseId) {
      that.setData({
        warehouse: e.warehouse,
        warehouseId: e.warehouseId
      });
    }
    that.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName +'-配送监控',
      path: '/pages/schedule/alarmMessage'
    }
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.server + '/wechat/wxapplet/alarmMessage',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        var data = res.data.data;
        if (res.data.success && data.length > 0) {
          that.setData({
            hasData: true,
            data: data
          });
        } else {
          that.setData({
            hasData: false,
            data: []
          })
        };
      },
      fail: function (error) {
        wx.hideLoading();
        console.log(error);
        that.setData({
          hasData: false,
          data: []
        })
      }
    });

  },
  alarm_type_select: function (e) {
    var that = this;
    var _menu = ['车辆超速报警', '路线异常报警', '超时停留报警', '油耗超标报警', '重大自然灾害预警（自动采集）', '全部报警'];
    wx.showActionSheet({
      itemList: _menu,
      success(res) {
        that.setData({
          alarm_type_text: _menu[res.tapIndex]
        })
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  }
})
